package commons;
import gui.commons.CellRenderer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class UserList implements Comparator<User> {
	private ArrayList<User> content;
	
	public UserList(){
		this.content = new ArrayList<User>();
	}

	public void addUser(User user) throws Exception{
		if(!this.content.contains(user)){
			this.content.add(user);
		}
		else {
			throw new Exception("Nickname not available. Please choose another one.");
		}
	}


	public void delUser(User user){
		this.content.remove(user);
	}
	
	public synchronized String getUserListString(){
		String str="";
		Iterator<User> itr=this.content.iterator();
		while(itr.hasNext()){
			str=str+itr.next().getName()+";";
		}
		return str.substring(0,str.length()-1);
	}

	
	public User getUser(String str){
		Iterator<User> itr=this.content.iterator();
		while(itr.hasNext()){
			User us=itr.next();
			if(us.getName().equals(str)){
				return us;
			}
		}
		return null;
	}

	
	public ArrayList<User> getAdminArrayList() {
		ArrayList<User> adminList = new ArrayList<User>();
		Iterator<User> itr=this.content.iterator();
		while(itr.hasNext()){
			User us=itr.next();
			if(us.isAdmin()){
				adminList.add(us);
			}
		}
		return adminList;
	}

	public ArrayList<User> getGuestArrayList() {
		ArrayList<User> guestList = new ArrayList<User>();
		Iterator<User> itr=this.content.iterator();
		while(itr.hasNext()){
			User us=itr.next();
			if(!us.isAdmin()){
				guestList.add(us);
			}
		}
		return guestList;
	}

	public ArrayList<User> getUserArrayList() {
		return this.content;
	}

	public boolean contains(User user){
		if(this.content.contains(user)){
			user.updateTimeStamp();
			return true;
		}
		else{
			return false;
		}
	}
	
	public User getUserFromId(int idSess){
		Iterator<User> i = this.content.iterator();
		while(i.hasNext()){
			User u = i.next();
			if(u.getIdSession()==idSess){
				return u;
			}
		}
		return null;
	}
	
	/*
	public synchronized String getUserListString(){
		String str="";
		Iterator<User> itr=this.iterator();
		while(itr.hasNext()){
			str=str+itr.next().getName()+"; ";
		}
		return str;
	}

	public synchronized String getAdminListString(){
		String str="";
		Iterator<User> itr=adminList.iterator();
		while(itr.hasNext()){
			str=str+itr.next().getName()+"; ";
		}
		return str;
	}
	
	
	public synchronized String getGuestListString(){
		String str="";
		Iterator<User> itr=guestList.iterator();
		while(itr.hasNext()){
			str=str+itr.next().getName()+"; ";
		}
		return str;
	}
	*/

	public synchronized User[] getUserListTab(){
		User[] table = new User[this.content.size()];
		Integer i=0;
		Iterator<User> itr=this.content.iterator();
		while(itr.hasNext()){
			table[i]=itr.next();
			i++;
		}
		return table;
	}

	public synchronized JTable getUserListJTab(){
		Object[][] data = {};
		String title[]={"user_name"};
		JTable table = new JTable(new DefaultTableModel(data,title));

		Iterator<User> itr=this.content.iterator();
		while(itr.hasNext()){
			String userName = itr.next().getName();

			DefaultTableModel modele = (DefaultTableModel)table.getModel();
			Object[] obj = new Object[1]; 
			obj[0] = userName;

			modele.addRow(obj);
			table.setModel(modele);
			table.repaint();
		}
		table.setDefaultRenderer(Object.class, new CellRenderer());
		return table;
	}

	public synchronized int getNumberOfUser(){
		return this.content.size();
	}

	public int compare(User user1, User user2) {
		return user1.compareTo(user2);
	}

}
